package sis.reporttest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-20 16:07
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import sis.report.RosterReporter;
import sis.studentinfo.*;

import java.io.*;


public class RosterReporterTest extends TestCase {

    private Session session;

    public void setUp() {
        session = CourseSession.create(new Course("ENGL", "101"), DateUtil.createDate(2003, 1, 6));

        session.enroll(new Student("A"));
        session.enroll(new Student("B"));


    }

    public void testRosterReportBuffer() throws IOException {
        String rosterReport = new RosterReporter(session).getReport();
        assertReportContents(rosterReport);

    }


    public void testRosterReportWriter() throws IOException {
        Writer writer = new StringWriter();
        new RosterReporter(session).writeReport(writer);
        String rosterReport = writer.toString();
        assertReportContents(rosterReport);

    }

    public void testFiledReport() throws Exception {
        final String fileName = "testFileReport.txt";
        try {
            delete(fileName);
            new RosterReporter(session).writeReport(fileName);
            StringBuffer buffer = new StringBuffer();
            String line;

            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            while ((line = reader.readLine()) != null) {
                buffer.append(line + RosterReporter.NEWLINE);
            }
            reader.close();

            System.out.println(buffer.toString());
        } finally {
            delete(fileName);
        }

    }

    private void assertReportContents(String rosterReport) {
        assertEquals(RosterReporter.ROSTER_REPORT_HEADER +
                RosterReporter.NEWLINE + "A" + RosterReporter.NEWLINE + "B" + RosterReporter.NEWLINE +
                RosterReporter.ROSTER_REPORT_FOOTER + "2个学生", rosterReport);
    }

    private void delete(String fileName) throws Exception {
        File file = new File(fileName);
        if (file.exists()) {
            file.delete();
        }
    }

}
